package com.binhthuan.catechist.client.grid;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.data.TrainerDS;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.google.gwt.user.client.ui.Anchor;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class CourseTrainersGrid extends AbstractGrid {

    public CourseTrainersGrid() {
    }

    public CourseTrainersGrid(TrainerDS dataSource) {
        super(dataSource);

        ListGridField id = new ListGridField(Parameter.ID);
        id.setHidden(true);
        id.setCanHide(false);

        ListGridField patronSaint = new ListGridField(Parameter.PATRON_SAINT);
        patronSaint.setTitle(ECApplication.getConstants().patronSaint());
        patronSaint.setWidth(70);

        ListGridField lastName = new ListGridField(Parameter.LAST_NAME);
        lastName.setTitle(ECApplication.getConstants().lastName());

        ListGridField firstName = new ListGridField(Parameter.FIRST_NAME);
        firstName.setTitle(ECApplication.getConstants().firstName());

        ListGridField birthday = new ListGridField(Parameter.BIRTHDAY);
        birthday.setTitle(ECApplication.getConstants().birthday());

        ListGridField gender = new ListGridField(Parameter.MALE);
        gender.setTitle(" ");
        gender.setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                boolean isMale = (value == null) ? false : Boolean.valueOf(value.toString());
                return Canvas.imgHTML(isMale ? ECIcons.getIcon("male.png") : ECIcons.getIcon("female.png"), 16, 16);
            }
        });
        gender.setWidth(20);

        ListGridField phone = new ListGridField(Parameter.PHONE);
        phone.setTitle(ECApplication.getConstants().phone());

        ListGridField email = new ListGridField(Parameter.EMAIL);
        email.setTitle(ECApplication.getConstants().email());
        email.setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                
                if (value != null) {
                    Anchor link = new Anchor(value.toString(), true);
                    link.setHref("mailto:" + value);
                    return link.toString();
                }
                return null;
            }
        });

        setFields(id, patronSaint, lastName, firstName, gender, birthday, phone, email);
    }
}
